package tp.pr5;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import jargs.gnu.CmdLineParser;
import tp.pr5.commands.exceptions.*;
import tp.pr5.console.GameControllerConsole;
import tp.pr5.gui.GameControllerGUI;
import tp.pr5.gui.MainWindow;
import tp.pr5.maploader.*;
import tp.pr5.console.Console;

public class Main {
	public static Game g;
	public static MainWindow main;

	public Main() {
	}

	/**
	 * Clase principal que nos inicia la clase Map dese una fichero de texto ,
	 * llamando a MapLoader() si no se ha podido iniciar el juego nos lo indica.
	 * Entrada: String[] (args) flujo de entrada para cargar el mapa de entrada
	 * de nuestro juego.
	 * 
	 * @throws WrongCommandFormatException
	 */
	public static void main(String[] args) throws IOException {
		CmdLineParser parser = new CmdLineParser();
		CmdLineParser.Option opcionMode = parser.addStringOption('i',
				"interface");
		CmdLineParser.Option opcionMap = parser.addStringOption('m', "map");
		try {
			parser.parse(args);
		} catch (CmdLineParser.OptionException e) {
			e.getMessage();
			System.exit(-1);
		}
		String modo = (String) parser.getOptionValue(opcionMode);
		String maux = (String) parser.getOptionValue(opcionMap);
		Map m = null;
		try {
			File archivo_aleer = new File(maux);
			FileInputStream fr = new FileInputStream(archivo_aleer);
			MapLoaderFromTxtFile m2 = new MapLoaderFromTxtFile();
			m = m2.loadMap(fr);
			g = new Game(m);

			if (modo.equals("console")) {
				GameControllerConsole gcc = new GameControllerConsole(g);
				Console c = new Console();
				gcc.registerGameObserver(c);
				gcc.registerMapObserver(c);
				gcc.registerPlayerObserver(c);
				gcc.runGame();
				c.gameQuit();
				System.exit(0);
			} else {
				if (modo.equals("swing")) {
					GameControllerGUI gcg = new GameControllerGUI(g);
					MainWindow ventanagrafica = new MainWindow(gcg);
					// ventanagrafica.setVisible(true);
					gcg.runGame();
				} else
					System.exit(-1);

			}
		} catch (FileNotFoundException e) {
			System.err.println("Error opening file");
		} catch (ArrayIndexOutOfBoundsException e) {
			String LINE_SEPARATOR = System.getProperty("line.separator");
			System.err.println("No map file specified." + LINE_SEPARATOR
					+ LINE_SEPARATOR + "Usage: tp.pr3.Main <mapFile>"
					+ LINE_SEPARATOR);
			System.exit(1);
		}
	}

	/** Notifies that the player examined a room */
	public void playerHasExaminedRoom(RoomInfo r) {
	}

	/** Notifies that the player entered a room coming from a given direction */
	public void roomEntered(Directions direction, RoomInfo targetRoom) {
	}

}